<?php


namespace app\ymsh\controller;


use app\model\AgentBalanceLog;
use app\model\CouponOrder;
use app\model\MovieOrder;
use app\model\TakeoutOrder;
use Carbon\Carbon;

class Stat extends BaseController
{

    public function statMonth()
    {
        //本月
        $thisMonth = CouponOrder::where('settle_type', SETTLE_TYPE_PROFIT)
            ->whereBetweenTime('order_created_at', Carbon::now()->startOfMonth(), Carbon::now()->endOfMonth())
            ->whereIn('status', [CouponOrder::C_STATUS_RECHARGE_SUCCESS, CouponOrder::C_STATUS_PROCESSING])
            ->sum('profit');

        //影票的
        $thisMonth += MovieOrder::where('status', MovieOrder::STATUS_PAY)
            ->whereBetweenTime('created_at', Carbon::now()->startOfMonth(), Carbon::now()->endOfMonth())
            ->whereIn('supplier_status', [2, 3, 4, 5])
            ->sum('profit');

        //外卖的 时间紧迫没用常量了
        $thisMonth += TakeoutOrder::whereIn('status', [1, 8])
            ->whereBetweenTime('created_at', Carbon::now()->startOfMonth(), Carbon::now()->endOfMonth())
            ->sum('profit');


        //上月
        $lastMonth = CouponOrder::where('settle_type', SETTLE_TYPE_PROFIT)
            ->whereBetweenTime('order_created_at', Carbon::now()->subMonth()->startOfMonth(), Carbon::now()->startOfMonth())
            ->whereIn('status', [CouponOrder::C_STATUS_RECHARGE_SUCCESS, CouponOrder::C_STATUS_PROCESSING])
            ->sum('profit');

        $lastMonth += MovieOrder::where('status', MovieOrder::STATUS_PAY)
            ->whereBetweenTime('created_at', Carbon::now()->subMonth()->startOfMonth(), Carbon::now()->startOfMonth())
            ->whereIn('supplier_status', [2, 3, 4, 5])
            ->sum('profit');

        $lastMonth += TakeoutOrder::whereIn('status', [1, 8])
            ->whereBetweenTime('created_at', Carbon::now()->subMonth()->startOfMonth(), Carbon::now()->startOfMonth())
            ->sum('profit');


        //上月结算预估
        $lastMonthSettle = CouponOrder::where('settle_type', SETTLE_TYPE_PROFIT)
            ->whereBetweenTime('created_at', Carbon::now()->subMonth()->startOfMonth(), Carbon::now()->startOfMonth())
            ->whereIn('status', [CouponOrder::C_STATUS_RECHARGE_SUCCESS])
            ->sum('profit');

        $lastMonthSettle += MovieOrder::where('status', MovieOrder::STATUS_PAY)
            ->whereBetweenTime('created_at', Carbon::now()->subMonth()->startOfMonth(), Carbon::now()->startOfMonth())
            ->whereIn('supplier_status', [5])
            ->sum('profit');

        $lastMonthSettle += TakeoutOrder::whereIn('status', [8])
            ->whereBetweenTime('created_at', Carbon::now()->subMonth()->startOfMonth(), Carbon::now()->startOfMonth())
            ->sum('profit');


        return success('', compact('thisMonth', 'lastMonth', 'lastMonthSettle'));
    }


    /**
     * 根据支付时间，获取结算时间
     * 结算时间：次月25号
     */
    private function getSettleDate($payTime)
    {
        $year = substr($payTime, 0, 4);
        $month = substr($payTime, 5, 2) + 1;
        return date('Y-m-d', mktime(0, 0, 0, $month, 25, $year));
    }

}